package com.example.main

import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentPagerAdapter
import androidx.viewpager.widget.ViewPager
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.example.common.RouterPath
import com.example.common.base.BaseActivity
import com.example.common.dialog.AppUpdateDialog
import com.example.common.tab.NormalTabView
import com.example.common.tab.RoundTabView
import com.example.main.adpater.MainPageAdapter
import com.example.main.databinding.ActivityMainBinding
import dagger.hilt.android.AndroidEntryPoint
import me.majiajie.pagerbottomtabstrip.NavigationController
import me.majiajie.pagerbottomtabstrip.item.BaseTabItem
import me.majiajie.pagerbottomtabstrip.listener.OnTabItemSelectedListener

@Route(path = RouterPath.PATH_MAIN)
@AndroidEntryPoint
class MainActivity : BaseActivity<ActivityMainBinding>(){

    private var fragments = arrayListOf<Fragment>()

    private lateinit var pageAdapter: MainPageAdapter

    private lateinit var mController : NavigationController

    override fun getLayoutId(): Int=R.layout.activity_main


    override fun initViews() {

      mController= mDataBinding.mainBottomNav.custom()
          .addItem(newNormalTabItem(R.drawable.home,R.drawable.home_selected,"首页"))
          .addItem(newNormalTabItem(R.drawable.user,R.drawable.user_selected,"社区"))
          .addItem(newRoundTabItem(R.drawable.add,R.drawable.add,"发布"))
          .addItem(newNormalTabItem(R.drawable.notify,R.drawable.msg_selected,"消息"))
          .addItem(newNormalTabItem(R.drawable.community,R.drawable.search_selected,"通知"))
          .build()

        //添加Fragment
//        fragments.add(HomeFragment())
//        fragments.add(CommuityFragment())
//        fragments.add(MessageFragment())
//        fragments.add(MineFragment())

        //方案二 使用ARouter框架来加载Fragment
        fragments.add(ARouter.getInstance().build(RouterPath.PATH_FRAGMENT_HOME).navigation() as Fragment)
        fragments.add(ARouter.getInstance().build(RouterPath.PATH_FRAGMENT_COMMUNITY).navigation() as Fragment)
        fragments.add(ARouter.getInstance().build(RouterPath.PATH_FRAGMENT_MSG).navigation() as Fragment)
        fragments.add(ARouter.getInstance().build(RouterPath.PATH_FRAGMENT_MINE).navigation() as Fragment)




        pageAdapter=MainPageAdapter(supportFragmentManager,fragments,FragmentPagerAdapter.BEHAVIOR_SET_USER_VISIBLE_HINT)
        mDataBinding.mainViewPage.adapter=pageAdapter
        //设置关联ViewPager
       // mController.setupWithViewPager(mDataBinding.mainViewPage)
        //设置tab的选中监听
        mController.addTabItemSelectedListener(object : OnTabItemSelectedListener{
            override fun onSelected(index: Int, old: Int) {
                when(index){
                    0,1-> mDataBinding.mainViewPage.currentItem=index
                    3,4->mDataBinding.mainViewPage.currentItem=index-1
                    2-> AppUpdateDialog(this@MainActivity).show()
                }
            }

            override fun onRepeat(index: Int) {

            }
        })
        //设置ViewPager前后缓存页面个数
        mDataBinding.mainViewPage.offscreenPageLimit=3


        mDataBinding.mainViewPage.addOnPageChangeListener(object : ViewPager.OnPageChangeListener{
            override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {

            }

            override fun onPageSelected(position: Int) {
               when(position){
                   0,1 ->mController.setSelect(position)
                   2,3->mController.setSelect(position+1)
               }
            }

            override fun onPageScrollStateChanged(state: Int) {

            }

        })


    }

    private fun newRoundTabItem(defaultDrawable : Int, checkDrawable : Int, title : String) : BaseTabItem{
        var roundTabItem =RoundTabView(this)
        roundTabItem.initData(defaultDrawable,checkDrawable,title)

        roundTabItem.setDefaultTextColor(R.color.black)
        roundTabItem.setSelectedTextColor(R.color.black)
        return roundTabItem
    }

    private fun newNormalTabItem(defaultDrawable : Int, checkDrawable : Int, title : String,hasMsg: Boolean = false, msgNum: Int = 0) : BaseTabItem{
        var roundTabItem =NormalTabView(this)
        roundTabItem.initData(defaultDrawable,checkDrawable,title)

        roundTabItem.setDefaultTextColor(R.color.black)
        roundTabItem.setSelectedTextColor(R.color.black)

        if (hasMsg && msgNum>0){
            roundTabItem.setHasMessage(true)
            roundTabItem.setMessageNumber(msgNum)
        }

        return roundTabItem
    }



}